package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.MaterialConstans;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;

import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.utils.UserIdThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.UUID;

/**
 * @Author：shi_xin_rui
 * @name：WmMaterialServiceImpl
 * @Date：2023/6/4 17:07
 */
@Service
@Slf4j
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public ResponseResult upload(MultipartFile file) {
        //1. 检查参数
        if (file == null || file.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2. 上传图片到minIO中
        String prefix = UUID.randomUUID().toString().replace("-", "");
        //获取到后缀
        String fileName = file.getOriginalFilename();
        String postfix = fileName.substring(fileName.lastIndexOf("."));
        String storeName = null;
        try {
            storeName = fileStorageService.uploadImgFile("", prefix + postfix, file.getInputStream());
            log.info("上传图片到MinIO中，storeName:{}",storeName);
        } catch (Exception e) {
            log.error("WmMaterialServiceImpl-上传文件失败");
            e.printStackTrace();
        }

        //3.保存到数据库中
        //3.1 构造文件对象
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(UserIdThreadUtil.get());
        wmMaterial.setUrl(storeName);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setType((short)0);
        wmMaterial.setCreatedTime(new Date());
        //3.2 存入数据库
        save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //1.检验参数
        dto.checkParam();
        //2.分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //3.构建查询的条件
        //如果isCollection并且为收藏
        if (dto.getIsCollection() != null && dto.getIsCollection() == 1){
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }

        //加入用户id的条件
        lambdaQueryWrapper.eq(WmMaterial::getUserId, UserIdThreadUtil.get());

        //按照时间查询
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        this.page(page, lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        responseResult.setCode(200);
        log.info("查询成功！！查询到{}条数据", page.getSize());
        return responseResult;
    }
}